package gxnu.edu.cn.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import gxnu.edu.cn.entity.Cuser;
import gxnu.edu.cn.entity.DTO.CuserLoginDTO;
import gxnu.edu.cn.entity.DTO.CuserRegisterDTO;
import gxnu.edu.cn.entity.vo.PasswordRestVo;
import gxnu.edu.cn.result.Result;
import gxnu.edu.cn.service.ICuserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author cq
 * @since 2025-05-07
 */

@RestController
@RequestMapping("/cuser")
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
public class CuserController {
   @Autowired
        private ICuserService cuserService;


//    /**
//     * 登录
//     * @param cuserLoginDTO
//     * @return
//     */
//    @PostMapping("/login")
//    @ApiOperation("登录")
//    public Result<Cuser> login(@RequestBody CuserLoginDTO cuserLoginDTO) {
//        // log.info("用户登录：{}", cuserLoginDTO);
//        Cuser user = cuserService.login(cuserLoginDTO);
//        if (user == null) {
//            return Result.error("用户名或密码错误");
//        }
//        Cuser cuserVO = new Cuser();
//        cuserVO.setUserID(user.getUserID());
//        cuserVO.setUserName(user.getUserName());
//        return Result.success(cuserVO);
//    }
//
//
//    /**
//         * 忘记口令重置验证
//         * @param
//         * @return
//         */
//        @GetMapping("/sendVerificationEmail")
//        public Result sendVerificationEmail(@RequestParam String userName) {
//            //String userName = payload.get("userName");
//
//            // 调用服务层发送邮件
//            boolean success = cuserService.sendVerificationEmail(userName);
//
//            if (!success) {
//                return Result.error("用户不存在或邮件发送失败");
//            }
//
//            return Result.success("邮箱发送成功，请注意查收！未完成当前默认为654321");
//        }
//
//        @PostMapping("/validEmailCode")
//        public Result validEmailCode(@RequestBody PasswordRestVo passwordRestVo){
//            int result = cuserService.validEmailCode(passwordRestVo.getUserName(), passwordRestVo.getVerificationCode());
//            if(result == 0)return Result.error("验证码错误或已过期！");
//            return Result.success("验证成功！");
//
//        }
//
//
//
//
    /**
     * 新建账户
     * @param registerDTO 包含用户名、密码和确认密码的注册信息
     * @return 新建用户的结果
     */
    @PostMapping("/addUser")
    @ApiOperation("新建账户")
    public Result<Cuser> add(@RequestBody CuserRegisterDTO registerDTO) {

        // 创建新用户对象
        Cuser newUser = new Cuser();
        newUser.setUserName(registerDTO.getUserName());
        newUser.setPassWord(registerDTO.getPassword());
        // 将权限角色定为用户
        newUser.setPermissionRole(true);

        // 调用服务层创建用户
        Cuser createdUser = cuserService.register(newUser);
        if (createdUser == null) {
            return Result.error("新建账户失败");
        }

        return Result.success(createdUser);
    }



    @GetMapping("/findByUserName")
    @ApiOperation("搜索")
    public Result<Cuser> searchUser(@RequestParam String userName) {
        Cuser user = cuserService.searchUserByUserName(userName);
        if (user == null) {
            return Result.error("未找到该用户名对应的账户");
        }
        return Result.success(user);
    }

    @GetMapping("/findAll")
    @ApiOperation("查询全部用户")
    public Result<List<Cuser>> findAllUsers() {
        List<Cuser> userList = cuserService.findAllUsers();
        return Result.success(userList);
    }


    }


